package springboot.example.tools;

import springboot.example.entity.ClientType;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class CommonUtils {

    /**
     * @Title: getIpAddr
     * @author kaka  www.zuidaima.com
     * @Description: 获取客户端IP地址
     * @param @return
     * @return String
     * @throws
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if(ip.equals("127.0.0.1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ip= inet.getHostAddress();
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if(ip != null && ip.length() > 15){
            if(ip.indexOf(",")>0){
                ip = ip.substring(0,ip.indexOf(","));
            }
        }
        return ip;
    }

    /**
     * 获取请求客户端类型
     * @param request
     * @return
     */
    public static ClientType getRequestClientType(HttpServletRequest request) {

        String userAgent = request.getHeader("User-Agent");
        if (userAgent == null) {
            return ClientType.WINDOWN;
        }
        if (userAgent.contains("Android")||userAgent.contains("Linux")) {
            //  Mozilla/5.0 (Linux; Android 4.4.2; Che2-UL00 Build/HonorChe2-UL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36
            System.err.println("Android移动客户端");
            if (userAgent.contains("MicroMessenger")) {
                //Mozilla/5.0 (Linux; Android 5.1; M3s Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044203 Mobile Safari/537.36 MicroMessenger/6.6.7.1321(0x26060739) NetType/WIFI Language/zh_CN
                System.err.println("微信");
                return ClientType.ANDROID_WEIXIN;
            }
            return ClientType.ANDROID;
        } else if (userAgent.contains("iPhone")) {
            System.err .println("iPhone移动客户端");
            if (userAgent.contains("MicroMessenger")) {
                System.err.println("微信");
                return ClientType.IPHONE_WEIXIN;
            }
            return ClientType.IPHONE;

        } else if (userAgent.contains("iPad")) {
            System.err.println("iPad客户端");
            if (userAgent.contains("MicroMessenger")) {
                System.err.println("微信");
                return ClientType.IPAD_WEXIN;
            }
            return ClientType.IPAD;
        } else if(userAgent.contains("Windows")){
            // Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36
            System.err.println("Windows");
            return ClientType.WINDOWN;
        }
        return ClientType.WINDOWN;
    }

}
